using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml.Serialization;
using System.IO;
using Sixeyed.CacheAdapter.Configuration.Tests.Properties;

namespace Sixeyed.CacheAdapter.Configuration.Tests
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class MessageCacheConfigurationCollectionTest
    {
        public MessageCacheConfigurationCollectionTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        [TestMethod]
        public void Deserialize()
        {
            string configString = Resources.MessageCacheConfigurationCollection;
            XmlSerializer serializer = new XmlSerializer(typeof(MessageCacheConfigurationCollection));
            MessageCacheConfigurationCollection configurationCollection = (MessageCacheConfigurationCollection)serializer.Deserialize(new StringReader(configString));
            Assert.IsNotNull(configurationCollection);
            Assert.AreEqual(3, configurationCollection.MessageCacheConfigurations.Length);
            foreach (MessageCacheConfiguration config in configurationCollection.MessageCacheConfigurations)
            {
                Assert.IsNotNull(config.MessageType);
            }
        }

        [TestMethod]
        public void IsMessageCacheEnabled()
        {
            string configString = Resources.MessageCacheConfigurationCollection;
            XmlSerializer serializer = new XmlSerializer(typeof(MessageCacheConfigurationCollection));
            MessageCacheConfigurationCollection configurationCollection = (MessageCacheConfigurationCollection)serializer.Deserialize(new StringReader(configString));
            Assert.IsTrue(configurationCollection.IsMessageCacheEnabled("http://schemas.xyz.com/2009#GetXYZ"));
        }

        [TestMethod]
        public void GetMessageCacheLifespan()
        {
            string configString = Resources.MessageCacheConfigurationCollection;
            XmlSerializer serializer = new XmlSerializer(typeof(MessageCacheConfigurationCollection));
            MessageCacheConfigurationCollection configurationCollection = (MessageCacheConfigurationCollection)serializer.Deserialize(new StringReader(configString));
            Assert.AreEqual(new TimeSpan(0, 10, 0), configurationCollection.GetMessageCacheLifespan("http://schemas.xyz.com/2009#GetXYZ"));
        }

        [TestMethod]
        public void Serialize()
        {
            MessageCacheConfiguration config = new MessageCacheConfiguration();
            config.MessageType = "http://schemas.xyz.com#GetXYZ";
            config.CacheLifespan = TimeSpan.Zero;
            List<MessageCacheConfiguration> configs = new List<MessageCacheConfiguration>(1);
            configs.Add(config);
            MessageCacheConfigurationCollection collection = new MessageCacheConfigurationCollection();
            collection.MessageCacheConfigurations = configs.ToArray();

            XmlSerializer serializer = new XmlSerializer(typeof(MessageCacheConfigurationCollection));
            MemoryStream stream = new MemoryStream();
            serializer.Serialize(stream, collection);
            stream.Position = 0;
            StreamReader reader = new StreamReader(stream);
            string configString = reader.ReadToEnd();

            Assert.IsFalse(string.IsNullOrEmpty(configString));
        }
    }
}
